#include <QCoreApplication>
#include <QDir>

#include "../config/config.h"
#include "../util/Singleton.h"
#include "../util/PhotoUtil.h"
#include "../global/QmlNames.h"
#include "RosterModel.h"
#include "LocalUserModel.h"

X_BEGIN_NAMESPACE

LocalUserModel::LocalUserModel()
    : QObject(NULL)
    , d(new LocalUserModelPrivate)
{
    // 初始化时使用默认头像
    d->m_photo = Config::instance().defaultAvatar();
    d->m_thumbnail = Config::instance().defaultAvatar();
}

LocalUserModel::LocalUserModel(const LocalUserModel &src)
    : QObject(NULL)
    , d(src.d)
{
}

LocalUserModel &LocalUserModel::operator =(const LocalUserModel &rhs)
{
    if(this != &rhs) d = rhs.d;

    return *this;
}

LocalUserModel &LocalUserModel::instance()
{
    return *Singleton<LocalUserModel>::instance(LocalUserModel::createInstance);
}

const char *LocalUserModel::qmlName()
{
    return QmlNames::TopLevelModelSingleton::LocalUserSingleton;
}

QObject *LocalUserModel::qmlInstance(QQmlEngine *engine, QJSEngine *jsEngine)
{
    Q_UNUSED(engine);
    Q_UNUSED(jsEngine);

    return &instance();
}

/**
 * @brief 更新自己的名片信息。
 */
void LocalUserModel::updateVCard()
{
    RosterModel::instance().saveContact(bareJid(), photo(), photoType(), "Friends");
    RosterModel::instance().updateContactVCard(bareJid(), vcard());
}

/**
 * @brief 获取名片信息。
 * @return
 */
VCard &LocalUserModel::vcard() const
{
    if(d->m_vcard == NULL){
        d->m_vcard = RosterModel::instance().loadVCard(bareJid());
    }

    return *d->m_vcard;
}

AvailableStatus LocalUserModel::availableStatus() const
{
    return d->m_availableStatus;
}

QString LocalUserModel::bareJid() const
{
    return d->m_bareJid;
}

QString LocalUserModel::password() const
{
    return d->m_password;
}

QDate LocalUserModel::birthday() const
{
    return vcard().m_birthday;
}

QString LocalUserModel::description() const
{
    return d->m_description;
}

QString LocalUserModel::telephone() const
{
    return vcard().m_telephone;
}

QString LocalUserModel::mobile() const
{
    return vcard().m_mobile;
}

QString LocalUserModel::email() const
{
    return vcard().m_email;
}

QString LocalUserModel::address() const
{
    return vcard().m_address;
}

QString LocalUserModel::firstName() const
{
    return vcard().m_firstName;
}

QString LocalUserModel::lastName() const
{
    return vcard().m_lastName;
}

QString LocalUserModel::nickName() const
{
    return d->m_nickName;
}

QString LocalUserModel::photo() const
{
    return d->m_photo;
}

PhotoType LocalUserModel::photoType() const
{
    return d->m_photoType;
}

QString LocalUserModel::url() const
{
    return d->m_vcard->m_url;
}

QString LocalUserModel::jid() const
{
    return d->m_jid;
}

QString LocalUserModel::thumbnail() const
{
    return d->m_thumbnail;
}

LocalUserModel& LocalUserModel::setAvailableStatus(AvailableStatus availableStatus)
{
    if (d->m_availableStatus == availableStatus)
        return *this;

    d->m_availableStatus = availableStatus;
    emit availableStatusChanged(availableStatus);

    return *this;
}

LocalUserModel& LocalUserModel::setPassword(QString password)
{
    if (d->m_password == password)
        return *this;

    d->m_password = password;
    emit passwordChanged(password);

    return *this;
}

LocalUserModel& LocalUserModel::setBirthday(QDate birthday)
{
    if (vcard().m_birthday == birthday)
        return *this;

    vcard().m_birthday = birthday;
    emit birthdayChanged(birthday);

    return *this;
}

LocalUserModel& LocalUserModel::setDescription(QString description)
{
    if (d->m_description == description)
        return *this;

    d->m_description = description;
    emit descriptionChanged(description);

    return *this;
}

LocalUserModel& LocalUserModel::setTelephone(QString telephone)
{
    if (vcard().m_telephone == telephone)
        return *this;

    vcard().m_telephone = telephone;
    emit telephoneChanged(telephone);

    return *this;
}

LocalUserModel& LocalUserModel::setMobile(QString mobile)
{
    if (vcard().m_mobile == mobile)
        return *this;

    vcard().m_mobile = mobile;
    emit mobileChanged(mobile);

    return *this;
}

LocalUserModel& LocalUserModel::setEmail(QString email)
{
    if (vcard().m_email == email)
        return *this;

    vcard().m_email = email;
    emit emailChanged(email);

    return *this;
}

LocalUserModel& LocalUserModel::setAddress(QString address)
{
    if (d->m_vcard->m_address == address)
        return *this;

    d->m_vcard->m_address = address;
    emit addressChanged(address);

    return *this;
}

LocalUserModel& LocalUserModel::setFirstName(QString firstName)
{
    if (vcard().m_firstName == firstName)
        return *this;

    vcard().m_firstName = firstName;
    emit firstNameChanged(firstName);

    return *this;
}

LocalUserModel& LocalUserModel::setLastName(QString lastName)
{
    if (vcard().m_lastName == lastName)
        return *this;

    vcard().m_lastName = lastName;
    emit lastNameChanged(lastName);

    return *this;
}

LocalUserModel& LocalUserModel::setNickName(QString nickName)
{
    if (d->m_nickName == nickName)
        return *this;

    d->m_nickName = nickName;
    emit nickNameChanged(nickName);

    return *this;
}

/**
 * @brief 设置照片的时候，顺便生成
 * @param photo
 * @return
 */
LocalUserModel& LocalUserModel::setPhoto(QString photo)
{
    QString temp = photo.startsWith("/") ? "file://" + photo : "file:///" + photo;
    if (d->m_photo == temp)
        return *this;

    QString thumbnailPhoto = PhotoUtil::instance().createThumbnail(photo, 60, 60);
    d->m_photo = temp;
    d->m_thumbnail = thumbnailPhoto.startsWith("/") ? "file://" + thumbnailPhoto : "file:///" + thumbnailPhoto;

    emit photoChanged(d->m_photo);
    emit thumbnailChanged(d->m_thumbnail);

    return *this;
}

LocalUserModel& LocalUserModel::setPhotoType(PhotoType photoType)
{
    if (d->m_photoType == photoType)
        return *this;

    d->m_photoType = photoType;
    emit photoTypeChanged(photoType);

    return *this;
}

LocalUserModel& LocalUserModel::setUrl(QString url)
{
    if (vcard().m_url == url)
        return *this;

    vcard().m_url = url;
    emit urlChanged(url);

    return *this;
}

LocalUserModel &LocalUserModel::setBareJid(QString bareJid)
{
    if (d->m_bareJid == bareJid)
        return *this;

    d->m_bareJid = bareJid;
    emit bareJidChanged(bareJid);

    return *this;
}

LocalUserModel &LocalUserModel::setJid(QString jid)
{
    if (d->m_jid == jid)
        return *this;

    d->m_jid = jid;
    emit jidChanged(jid);

    return *this;
}

LocalUserModel *LocalUserModel::createInstance()
{
    return new LocalUserModel();
}

X_END_NAMESPACE
